package com.chixing.day02;

/**
 * 提供排序工具类
 * 提供了常用的排序算法，如冒泡排序、选择排序、快速排序、插入排序
 */


public class SortUtil {
    /**
     * 冒泡排序
     * @Param: original 要排序的原始数组
     * @Returns: 返回排序好的数组
     */
    public static int[] buBboSort(int[] original){
        for (int i = 0; i < original.length; i++) {
            for (int j = i+1; j < original.length; j++) {
                if(original[i]>original[j]){
                    original[i]=original[i]^original[j];
                    original[j]=original[i]^original[j];
                    original[i]=original[i]^original[j];
                }
            }
        }
        return original;
    }

    /**
     * 快速排序
     * @Param: original 要排序的原始数组
     * @Returns: 返回排序好的数组
     */
    public static int[] quickSort(int[] original,int low,int high){
        if(low<high){
            int mid = midNumber(original,low,high);
            quickSort(original,low,mid-1);
            quickSort(original,mid+1,high);
        }
        return original;
    }
    public static int midNumber(int[] original,int low,int high){
        int temp = original[low];
        while (low<high){
            while(low<high&&original[high]>temp) high--;
            original[low] = original[high];
            while (low<high&&original[low]<temp) low++;
            original[high] = original[low];
        }
        original[low]=temp;
        int mid = low;
        return mid;
    }

}

